#!/usr/bin/env python
from threading import Thread
from httplib import HTTPConnection,HTTPSConnection
from pysqlite2 import dbapi2 as sqlite
from sys import argv
class testit(Thread):
	def __init__(self,urls):
		Thread.__init__(self)
		self.urls=urls
		pass
	def run(self):
		for i in self.urls:
			url=i[0]
			if url.find("google.")==-1:
				if url.startswith("https"):
					status=self.httpstest(url)
				else:
					status=self.httptest(url)
				if status!=200 and status!=302:
					print "%s: %d"%(url,status)
	def httptest(self,url):
		conn=HTTPConnection(url.split("://")[1].split("/")[0])
		conn.putrequest("GET","/"+url.split("://")[1].split("/",1)[1])
		conn.endheaders()
		r=conn.getresponse()
		return r.status

	def httpstest(self,url):
		conn=HTTPSConnection(url.split("://")[1].split("/")[0])
		conn.putrequest("GET","/"+url.split("://")[1].split("/",1)[1])
		conn.endheaders()
		r=conn.getresponse()
		return r.status


def main():
	conn=sqlite.connect(argv[1])
	sql="select moz_places.url from moz_places inner join moz_bookmarks on moz_places.id=moz_bookmarks.fk where moz_bookmarks.type=1 and moz_places.url like 'http%';"
	cursor=conn.cursor()
	cursor.execute(sql)
	urls=cursor.fetchall()
	cursor.close()
	conn.close()
	amount=len(urls)/10
	while len(urls)>0:
		urllist=[]
		for i in range(amount):
			if len(urls)>0:
				urllist.append(urls.pop())
		current=testit(urllist)
		current.start()

main()
